package com.example.administrator.myapplication.find.assortment;


import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pair;
import android.support.v7.widget.GridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.administrator.myapplication.BR;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.callback.XmlyService;
import com.example.administrator.myapplication.databinding.FragmentAssortmentBinding;
import com.example.administrator.myapplication.find.assortment.bean.assortmentEntity.AssortmentEntity;
import com.example.administrator.myapplication.find.assortment.bean.assortmentEntity.AssortmentTitle;
import com.example.administrator.myapplication.find.assortment.bean.bottomEntity.BottomEntity;
import com.example.administrator.myapplication.until.BaseApplication;
import com.example.administrator.myapplication.until.CommentRecyclerAdapter;

import java.util.ArrayList;
import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class AssortmentFragment extends Fragment implements Callback<AssortmentEntity> {
    private FragmentAssortmentBinding binding;
    private CommentRecyclerAdapter<Object> adapter;
    private XmlyService service;
    private int mPosition =0;

    public AssortmentFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return DataBindingUtil.inflate(inflater, R.layout.fragment_assortment, container, false).getRoot();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding = DataBindingUtil.getBinding(view);
        GridLayoutManager manager = new GridLayoutManager(getContext(), 2);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 0) {
                    return 2;
                }else if(position==mPosition){
                    return  2;
                }
                return 1;
            }
        });
        binding.recycler.setLayoutManager(manager);
        HashMap<Class<?>, Pair<Integer, Integer>> map = new HashMap<>();
        map.put(String.class, Pair.create(R.layout.assortment_image_top, BR.assprtmentString));
        map.put(AssortmentTitle.class, Pair.create(R.layout.assortment_title, BR.assortmentTitle));
        map.put(BottomEntity.class, Pair.create(R.layout.heard_line_viewpager_item, BR.bottomEntity1));
        adapter = new CommentRecyclerAdapter<>(getContext(), new ArrayList<>(), map);
        binding.recycler.setAdapter(adapter);
        service = ((BaseApplication) getContext().getApplicationContext()).getService();
        service.getAssortmentEntity().enqueue(this);
    }

    @Override
    public void onResponse(Call<AssortmentEntity> call, Response<AssortmentEntity> response) {
        String s = response.body().getList().get(0).getCoverPath();
        adapter.add(0, s);
        AssortmentEntity body = response.body();
        mPosition = body.getList().size();
        body.getList().remove(0);
        adapter.addAll(body.getList());
         aboutBottomViewPager();
    }

    @Override
    public void onFailure(Call<AssortmentEntity> call, Throwable t) {
        t.getMessage();
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }

    private void aboutBottomViewPager() {
        XmlyService xmlyService = new Retrofit.Builder()
                .baseUrl("http://adse.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(XmlyService.class);
        xmlyService.getBottomEntity().enqueue(new Callback<BottomEntity>() {
            @Override
            public void onResponse(Call<BottomEntity> call, Response<BottomEntity> response) {
                adapter.add(response.body());
            }

            @Override
            public void onFailure(Call<BottomEntity> call, Throwable t) {
                t.getMessage();
                Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
